package com.qf.user.service.impl;

import com.qf.base.util.PatternUtil;
import com.qf.user.dao.UserDao;
import com.qf.user.dao.UserMapper;
import com.qf.user.exception.SignInIsLockedException;
import com.qf.user.exception.SignInNameUnknownException;
import com.qf.user.exception.SignInPasswordErrorException;
import com.qf.user.exception.base.UserException;
import com.qf.user.model.User;
import com.qf.user.model.UserExample;
import com.qf.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author 汤奇
 * @version V1.0
 * @Project BookPro
 * @Package com.qf.user.service.impl
 * @Description:
 * @Date 2022/7/29 20:40
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserDao userDao;

    @Override
    public User signIn(String signInName, String password) throws UserException{
        User user = isLocked(signInName);
        if (user == null){
            //登录名不在数据库中
            throw new SignInNameUnknownException();
        }
        if (user.getIsLock() == 1){
            //被锁定了
            throw new SignInIsLockedException();
        }
        if (user.getPassword().equals(password)){
            //密码输入正确
            return user;
        } else {
            //密码错了
            Date errorTime = user.getErrorTime();
            if (errorTime == null){
                //清空, 从1开始记
                user.setErrorTime(new Date());
                user.setTryCount(1);
                //更新数据库
                userDao.updateByPrimaryKeySelective(user);
                throw new SignInPasswordErrorException();
            }
            SimpleDateFormat sdf = new SimpleDateFormat();
            sdf.applyPattern("yyyyMMdd");
            String dbDay = sdf.format(errorTime);
            String nowDay = sdf.format(new Date());
            if (dbDay.equals(nowDay)){
                //累计次数
                user.setTryCount(user.getTryCount()+1);
                Integer count = 3;
                if (user.getTryCount().equals(count)){
                    //设置账户为锁定状态
                    user.setIsLock(1);
                }
            } else {
                //清空, 从1开始记
                user.setErrorTime(new Date());
                user.setTryCount(1);
            }
        }
        //更新数据库
        userDao.updateByPrimaryKeySelective(user);
       throw new SignInPasswordErrorException();
    }

    @Override
    public User isLocked(String signInName) {
        //塑料模特
        UserExample example = new UserExample();
        //条件对象
        UserExample.Criteria criteria = example.createCriteria();

        //判断用户输入的是 手机号, 用户名, 还是邮箱
        boolean isMobile = PatternUtil.isMobile(signInName);
        if (isMobile){
            //手机号
            criteria.andTelephoneEqualTo(signInName);
        } else {
            boolean isEmail = PatternUtil.isEmail(signInName);
            if (isEmail){
                //邮箱
                criteria.andEmailEqualTo(signInName);
            } else {
                //用户名
                criteria.andUsernameEqualTo(signInName);
            }
        }

        //根据塑料模特查询数据
        List<User> users = userDao.selectByExample(example);
        if (users!=null && users.size()>0) {
            //查到数据了
            User user = users.get(0);
            Date errorTime = user.getErrorTime();
            if (errorTime != null) {
                //判断错得日期是不是今天
                SimpleDateFormat sdf = new SimpleDateFormat();
                sdf.applyPattern("yyyyMMdd");
                String dbDay = sdf.format(errorTime);
                String nowDay = sdf.format(new Date());
                if (!dbDay.equals(nowDay)){
                    //输错这件事不是今天发生的
                    //应该重置次数
                    user.setTryCount(0);
                    user.setIsLock(0);
                    userDao.updateByPrimaryKeySelective(user);
                }
            } else {
                //没错过
                user.setTryCount(0);
                user.setIsLock(0);
                userDao.updateByPrimaryKeySelective(user);
            }
            return user;
        }
        return null;
    }

    @Override
    public User findOrSIgnUp(String phone) {
        //1. 用户手机号查用户
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        criteria.andTelephoneEqualTo(phone);
        //查询
        List<User> users = userDao.selectByExample(userExample);
        if (users != null && users.size() >0){
            return users.get(0);
        } else {
            //2.没查到
            User user = new User();
            user.setTelephone(phone);
            //随机昵称
            String nickName = userMapper.randomNickName();
            user.setNickname(nickName);
            //插入数据
            userDao.insertSelective(user);
            users = userDao.selectByExample(userExample);

            return users.get(0);
        }
    }

    @Override
    public int updateIconByUserId(Long userId, String url) {
        User user = new User();
        user.setId(userId);
        user.setIconLink(url);

        return userDao.updateByPrimaryKeySelective(user);
    }
}
